package com.websuites.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.websuites.core.request.WsHttpServletRequest;
import com.websuites.core.response.WsHttpServletResponse;
import com.websuites.web.servlet.request.WsHttpServletRequestWrapper;
import com.websuites.web.servlet.request.WsHttpServletRequestWrapperDistributed;
import com.websuites.web.servlet.response.WsHttpServletResponseWrapper;

public class RequestHeaderFilter implements Filter
{
	private String environment = "singleton";
	
	public void init(FilterConfig config) throws ServletException
	{
		environment = config.getInitParameter("environment");
		if (environment == null)
		{
			environment = "singleton";
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
	{
		if (environment.equals("distributed"))
		{
			WsHttpServletRequest wsRequest = new WsHttpServletRequestWrapperDistributed((HttpServletRequest)request);
			WsHttpServletResponse wsResponse = new WsHttpServletResponseWrapper((HttpServletResponse)response);
			filterChain.doFilter(wsRequest, wsResponse);
			return;
		}
		
		WsHttpServletRequest wsRequest = new WsHttpServletRequestWrapper((HttpServletRequest)request);
		WsHttpServletResponse wsResponse = new WsHttpServletResponseWrapper((HttpServletResponse)response);
		filterChain.doFilter(wsRequest, wsResponse);			
	}

	public void destroy()
	{
	}
}
